package com.nwcl.basic.service.impl;

import com.nwcl.basic.bo.GoodsBO;
import com.nwcl.basic.dao.*;
import com.nwcl.basic.entity.Goods;
import com.nwcl.basic.service.GoodsService;
import com.nwcl.common.constant.Constant;
import com.nwcl.common.result.Page;
import com.nwcl.common.utils.BeanUtil;
import com.nwcl.ysx.request.GoodsPageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liguiqin
 * @date 2023/5/23
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private GoodsIntegralDao goodsIntegralDao;
    @Autowired
    private GoodsSpaceDao goodsSpaceDao;
    @Autowired
    private GoodsSalePriceDao goodsSalePriceDao;
    @Autowired
    private ResourcesDao resourcesDao;

    @Override
    public Page<GoodsBO> page(GoodsPageRequest param) {
        Page<GoodsBO> resultPage = new Page<>();
        List<Goods> modelList = goodsDao.list(param.getStatus());
        if (CollectionUtils.isEmpty(modelList)) {
            resultPage = new Page<>(param.getPageNum(), param.getPageSize(), 0, new ArrayList<>());
            return resultPage;
        }
        List<GoodsBO> boList = BeanUtil.copyList(modelList, GoodsBO.class);
        //手工设置分页信息
        resultPage = new Page(param.getPageNum(), param.getPageSize(), boList.size(), boList);
        resultPage = Page.getPageList(resultPage);
        boList = resultPage.getList();
        //设置商品积分
        setGoodsIntegral(boList);
        //设置商品库存
        setGoodsInventory(boList);
        //设置单价
        setGoodsPrice(boList);
        return resultPage;
    }

    @Override
    public GoodsBO getById(Integer id) {
        Goods goods =  goodsDao.getById(id);
        if(goods==null){
            return null;
        }
        List<GoodsBO> boList=new ArrayList<>();
        boList.add(BeanUtil.copy(goods,GoodsBO.class));
        setGoodsInventory(boList);
        setGoodsIntegral(boList);
        setGoodsPrice(boList);
        return boList.get(0);
    }

    /*
     * @Description:设置商品单价
     * @Author liguiqin
     * @Date 2023/5/25 16:16
     * @param: boList
     * @return void
     * @UpdateDate 2023/5/25 16:16
     **/
    private void setGoodsPrice(List<GoodsBO> boList) {
        List<Integer> goodsIds = new ArrayList<>();
        for (GoodsBO bo : boList) {
            goodsIds.add(bo.getId());
        }
        goodsSalePriceDao.listByGoodsIds(goodsIds).forEach(goodsPrice -> {
            boList.forEach(bo -> {
                if (bo.getId().equals(goodsPrice.getGoodsId())) {
                    bo.setPrice(goodsPrice.getPrice());
                }
            });
        });
    }


    /*
     * @Description: 设置商品库存
     * @Author liguiqin
     * @Date 2023/5/25 15:41
     * @param: boList
     * @return void
     * @UpdateDate 2023/5/25 15:41
     **/
    private void setGoodsInventory(List<GoodsBO> boList) {
        List<Integer> goodsIds = new ArrayList<>();
        for (GoodsBO bo : boList) {
            goodsIds.add(bo.getId());
        }
        goodsSpaceDao.listByGoodsIds(goodsIds).forEach(goodsSpace -> {
            boList.forEach(bo -> {
                if (bo.getId().equals(goodsSpace.getGoodsId())) {
                    bo.setInventory(goodsSpace.getInventory());
                }
            });
        });
    }

    /*
     * @Description: 设置商品积分
     * @Author liguiqin
     * @Date 2023/5/25 15:41
     * @param: boList
     * @return void
     * @UpdateDate 2023/5/25 15:41
     **/
    private void setGoodsIntegral(List<GoodsBO> boList) {
        List<Integer> goodsIds = new ArrayList<>();
        for (GoodsBO bo : boList) {
            goodsIds.add(bo.getId());
        }
        goodsIntegralDao.listByGoodsIds(goodsIds).forEach(goodsIntegral -> {
            boList.forEach(bo -> {
                if (bo.getId().equals(goodsIntegral.getGoodsId())) {
                    bo.setIntegral(goodsIntegral.getIntegral());
                }
            });
        });
    }
}
